package cn.bone.ou.data.service.impl;

import cn.bone.base.annotation.MyLogger;
import cn.bone.base.auth.JWTInfo;
import cn.bone.base.bean.CommonResult;
import cn.bone.base.bean.DataGrid;
import cn.bone.base.bean.Ref;
import cn.bone.base.utils.TimeUtil;
import cn.bone.ou.community.bean.CommunityMng;
import cn.bone.ou.community.mapper.CommunityMngMapper;
import cn.bone.ou.data.bean.CommunityArticle;
import cn.bone.ou.data.bean.CommunityArticleDetail;
import cn.bone.ou.data.mapper.CommunityArticleDetailMapper;
import cn.bone.ou.data.mapper.CommunityArticleMapper;
import cn.bone.ou.data.service.IArticleService;
import cn.bone.ou.data.param.ArticleParam;
import cn.bone.ou.data.vo.ArticleVo;
import cn.bone.ou.vote.bean.VoteExtra;
import cn.bone.ou.vote.mapper.VoteExtraMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ArticleServiceImpl implements IArticleService {

    @Resource
    private CommunityArticleMapper articleMapper;
    @Resource
    private CommunityArticleDetailMapper articleDetailMapper;
    @Resource
    private CommunityMngMapper mngMapper;
    @Resource
    private VoteExtraMapper voteExtraMapper;

    /**分页获取列表*/
    public CommonResult<DataGrid<ArticleVo.ListVo>> list(ArticleParam.ListParam param){
        List<CommunityArticle> list = articleMapper.list(param.getCommunityId(),
                param.getStart(), param.getNum(),
                param.getTitle());
        int count = articleMapper.countList(param.getCommunityId(),param.getTitle());
        List<ArticleVo.ListVo> vos = new ArrayList<>();
        for(CommunityArticle article : list){
            ArticleVo.ListVo vo = new ArticleVo.ListVo();
            vo.setArticleId(article.getId());
            vo.setTitle(article.getTitle());
            vo.setCreateTime(TimeUtil.formaterYYYYMMddHHmm(article.getCreateTime()));
            CommunityMng mng = mngMapper.selectById(article.getCreateUserId());
            if(mng!=null){
                vo.setCreateUserName(mng.getRealName());
            }
            vos.add(vo);
        }
        DataGrid<ArticleVo.ListVo> dataGrid = new DataGrid<>();
        dataGrid.setTotal(count);
        dataGrid.setRows(vos);
        return Ref.ok(dataGrid);
    }

    /**新增文章*/
    @MyLogger(msg = "新增文章")
    public CommonResult<Void> add(ArticleParam.AddParam param){

        JWTInfo jwtInfo = JWTInfo.get();
        CommunityArticle article = new CommunityArticle();
        article.setCommunityId(param.getCommunityId());
        article.setTitle(param.getTitle());
        article.setCreateTime(new Date());
        article.setCreateUserId(jwtInfo.getUserId());
        articleMapper.insert(article);

        CommunityArticleDetail detail = new CommunityArticleDetail();
        detail.setArticleId(article.getId());
        detail.setContent(param.getContent());
        articleDetailMapper.insert(detail);

        return Ref.ok();
    }

    /**修改文章*/
    @MyLogger(msg = "修改文章")
    public CommonResult<Void> modify(ArticleParam.ModifyParam param){
        CommunityArticle article = articleMapper.selectById(param.getArticleId());
        if(article==null){
            return Ref.error("错误的文章id");
        }
        CommunityArticleDetail detail = articleDetailMapper.getByArticleId(param.getArticleId());
        if(detail==null){
            return Ref.error("文章数据错误");
        }
        JWTInfo jwtInfo = JWTInfo.get();
        article.setTitle(param.getTitle());
        article.setUpdateTime(new Date());
        article.setUpdateUserId(jwtInfo.getUserId());
        articleMapper.updateById(article);

        detail.setContent(param.getContent());
        articleDetailMapper.updateById(detail);

        return Ref.ok();
    }

    /**文章详情*/
    public CommonResult<ArticleVo.DetailVo> detail(ArticleParam.DeleteParam param){
        CommunityArticle article = articleMapper.selectById(param.getArticleId());
        if(article==null){
            return Ref.error("错误的文章id");
        }
        CommunityArticleDetail detail = articleDetailMapper.getByArticleId(param.getArticleId());
        if(detail==null){
            return Ref.error("文章数据错误");
        }
        ArticleVo.DetailVo vo = new ArticleVo.DetailVo();
        vo.setArticleId(article.getId());
        vo.setTitle(article.getTitle());
        vo.setContent(detail.getContent());
        vo.setCreateTime(TimeUtil.formaterYYYYMMddHHmm(article.getCreateTime()));
        CommunityMng mng = mngMapper.selectById(article.getCreateUserId());
        if(mng!=null){
            vo.setCreateUserName(mng.getRealName());
        }
        return Ref.ok(vo);
    }

    /**删除文章*/
    @MyLogger(msg = "删除文章")
    public CommonResult delete(ArticleParam.DeleteParam param){

        CommunityArticle article = articleMapper.selectById(param.getArticleId());
        if(article==null){
            return Ref.error("错误的文章id");
        }
        CommunityArticleDetail detail = articleDetailMapper.getByArticleId(param.getArticleId());
        if(detail==null){
            return Ref.error("文章数据错误");
        }

        //判断文章是否有被引用
        VoteExtra extra = voteExtraMapper.hasArticle(article.getId());
        if(extra!=null){
            return Ref.error("编号["+extra.getVoteId()+"]的投票引用了该文章,不能删除");
        }

        articleMapper.deleteById(param.getArticleId());
        articleDetailMapper.deleteById(detail.getId());
        return Ref.ok();
    }
}
